package oot.activity;

import org.json.JSONException;
import org.json.JSONObject;

import com.androidquery.AQuery;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;

import oot.data.Connection;
import oot.data.User;
import oot.tab.activity.R;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Toast;

public class ProfileActivityBAK extends FragmentActivity {

	private User user;
	private AQuery aQuery;
	private String response;
	private int followers;
	private int followings;
	private int tweets;

	@SuppressLint("ParserError")
	@Override
	protected void onCreate(Bundle arg0) {
		// TODO Auto-generated method stub
		super.onCreate(arg0);
		setContentView(R.layout.profile);
		aQuery = new AQuery(ProfileActivityBAK.this);
		user = (User) getIntent().getSerializableExtra("user");
		aQuery.id(R.id.p_screenname).text(user.getUserScreenName());
		aQuery.id(R.id.p_username).text("@" + user.getUserName());
		aQuery.id(R.id.p_avatar).image(user.getUserAvatarURL(), true, true, 0,
				R.drawable.m05_item_avar);

		aQuery.id(R.id.p_btnfollower).getButton()
				.setOnClickListener(new OnClickListener() {

					@Override
					public void onClick(View paramView) {
						// TODO Auto-generated method stub
						Intent i = new Intent(getApplicationContext(),
								ListFollowersActivity.class);
						i.putExtra("get_userid", user.getUserID());
						startActivity(i);
					}
				});

		aQuery.id(R.id.p_btnfollowing).getButton()
				.setOnClickListener(new OnClickListener() {

					@Override
					public void onClick(View paramView) {
						// TODO Auto-generated method stub
						Intent i = new Intent(getApplicationContext(),
								ListFollowingsActivity.class);
						i.putExtra("get_userid", user.getUserID());
						startActivity(i);
					}
				});
		getUserInfo();
	}

	@SuppressWarnings("static-access")
	public void getUserInfo() {

		Connection connection = new Connection();
		RequestParams requestParams = new RequestParams();
		requestParams.put("get_userid", user.getUserID());
		connection.post("users/getusers", requestParams,
				new AsyncHttpResponseHandler() {
					@Override
					public void onSuccess(String res) {
						// TODO Auto-generated method stub
						super.onSuccess(res);
						response = res;
						contentPaser(response);
					}

					@Override
					public void onFailure(Throwable arg0, String res) {
						// TODO Auto-generated method stub
						super.onFailure(arg0, res);
						response = null;
						contentPaser(response);
					}
				});

	}

	public void contentPaser(String pString) {
		if (pString == null) {
			Toast.makeText(getApplicationContext(), "Get user infor faile",
					Toast.LENGTH_LONG).show();
		} else {
			JSONObject jsonObject;
			try {
				jsonObject = new JSONObject(pString);
				if (jsonObject.getInt("status_code") == 0) {
					JSONObject js = jsonObject.getJSONArray("entity")
							.getJSONObject(0);
					followers = js.getInt("followers");
					followings = js.getInt("followings");
					tweets = js.getInt("tweets");
					aQuery.id(R.id.p_follower).text("" + followers);
					aQuery.id(R.id.p_following).text("" + followings);
					aQuery.id(R.id.p_tweet).text("" + tweets);
				}
			} catch (JSONException e) {
				e.printStackTrace();
			}

		}
	}
}
